package com.vhall.component.controller.account.api;


import com.vhall.component.controller.account.facade.api.ApiCslAuthFacade;
import com.vhall.component.entity.account.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.framework.common.exception.GlobalErrorCode;
import com.vhall.component.plugin.common.annotation.ApiSignatureAuth;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.account.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * app用户 - 登录、登出、获取登录用户详情
 *
 * @author dalong
 * @date 2021-04-25 16:46
 */
@VhallComponent
public class ApiCslAuthController implements ApiCslAuthFacade {

    @Autowired
    private AccountService accountService;


    /**
     * 登录
     *
     * @param authLoginVO 参数
     * @return
     */
    @ApiSignatureAuth
    @Override
    public AuthLoginRspVO login(@Validated AuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.authLogin(authLoginVO, request, response, true);
    }


    /**
     * 退出登录
     *
     * @param authLogoutVO 参数
     */
    @Override
    public void logout(@Validated AuthLogoutVO authLogoutVO) {
        accountService.authLogout(authLogoutVO);
    }


    /**
     * 观众端登录
     *
     * @param authLoginVO 参数
     * @return
     */
    @Override
    public AuthLoginRspVO loginWatch(@Validated AuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.authLogin(authLoginVO, request, response, true);
    }


    /**
     * 第三方登录
     *
     * @param authLoginVO 参数
     * @return
     */
    @Override
    public ThirdAuthLoginRspVO thirdLogin(@Validated ThirdAuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.thirdAuthLogin(authLoginVO, request, response);
    }


    /**
     * 游客登录
     *
     * @param authLoginVO 参数
     * @return
     */
    @Override
    public VisitorAuthLoginRspVO visitor(@Validated VisitorAuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.visitor(authLoginVO, request, response);
    }

    @Override
    public VisitorAuthLoginRspVO visitorUseDeviceId(VisitorAuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.visitorUseDeviceId(authLoginVO, request, response);
    }

    @Override
    public List<NicknamesVO> getRandomNicknames() {
        return accountService.getRandomNicknames();
    }

    @Override
    @VssTokenAuth
    public boolean chooseNickname(ChooseNicknameVO chooseNicknameVO) {
        return accountService.chooseNickname(chooseNicknameVO);
    }

    /**
     * 查询账户信息
     *
     * @param vo 参数
     * @return
     */
    @ApiSignatureAuth
    @TokenAuth
    @Override
    public String set(AccountEditVO vo) {
        Token token = TokenThreadLocal.getToken();
        if (accountService.editNickName(token.getAccountId(), vo)) {
            return vo.getNickname();
        }
        throw new BusinessException(GlobalErrorCode.FAILED);
    }
}
